package arithmetic;

public class FibonacciSequence {
    /**
     * 斐波那契数列（黄金分割数列/兔子数列）
     * 从第3项开始，每一项的数据都等于前两项之和
     * 1,2,3,5,8,13......
     */

    public static void main(String[] args) {
        FibonacciSequence fibonacciSequence = new FibonacciSequence();
        int number = fibonacciSequence.getNumber(6);
        System.out.println(number);
        String str=fibonacciSequence.getString(10);
        System.out.println(str);

    }

    //求1~n的斐波那契数列
    public String getString(int n) {
        String str = "";
        for (int i = 1; i <= n; i++) {
            if (i == n) {
                str += getNumber(i);
            } else {
                str += getNumber(i) + ",";
            }
        }
        return str;
    }

    //求斐波那契数列第N项的数值
    public int getNumber(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;
        int sum = 0;
        for (int i = n; i > 2; i--) {
            if (i > 2) {
                sum = getNumber(n - 1) + getNumber(n - 2);
            }
        }
        return sum;
    }

}
